package com.baoyi.city;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import com.baoyi.http.Connection;
import com.baoyi.http.HttpConnection;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class CityUtils {
	static String app = "http://www.weather.com.cn/data/city3jdata/china.html";

	//http://www.weather.com.cn/data/weather_zs/101010100.html
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		List<Provshi> ps = china();
		for (Provshi provshi : ps) {
			System.out.println(provshi);
		}
		List<City> cs = china("10105");
		for (City provshi : cs) {
			System.out.println(provshi);
		}
		List<Station> ss = findStationByCode("1010100");
		for (Station provshi : ss) {
			System.out.println(provshi);
		}
	}

	public static List<Provshi> china() throws IOException {
		Connection connection = HttpConnection.connect(app);
		connection.header("Referer", "http://www.weather.com.cn/");
		connection.header("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
		String msg = connection.execute().body();
		JsonParser parser = new JsonParser();
		List<Provshi> datas = new ArrayList<Provshi>();
		JsonElement jsons = parser.parse(msg);
		JsonObject ob = jsons.getAsJsonObject();
		Set<Entry<String, JsonElement>> ss = ob.entrySet();
		for (Entry<String, JsonElement> entry : ss) {
			Provshi item = new Provshi();
			item.setCode(entry.getKey());
			item.setName(entry.getValue().getAsString());
			datas.add(item);

		}
		return datas;
	}

	public static List<City> china(String code) throws IOException {
		Connection connection = HttpConnection
				.connect("http://www.weather.com.cn/data/city3jdata/provshi/"
						+ code + ".html");
		connection.header("Referer", "http://www.weather.com.cn/");
		connection.header("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
		String msg = connection.execute().body();
		JsonParser parser = new JsonParser();
		List<City> datas = new ArrayList<City>();
		JsonElement jsons = parser.parse(msg);
		JsonObject ob = jsons.getAsJsonObject();
		Set<Entry<String, JsonElement>> ss = ob.entrySet();
		for (Entry<String, JsonElement> entry : ss) {
			City item = new City();
			item.setCode(entry.getKey());
			item.setName(entry.getValue().getAsString());
			datas.add(item);

		}
		return datas;
	}
	public static List<Station> findStationByCode(String code) throws IOException {
		Connection connection = HttpConnection
				.connect("http://www.weather.com.cn/data/city3jdata/station/"
						+ code + ".html");
		connection.header("Referer", "http://www.weather.com.cn/");
		connection.header("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
		String msg = connection.execute().body();
		JsonParser parser = new JsonParser();
		List<Station> datas = new ArrayList<Station>();
		JsonElement jsons = parser.parse(msg);
		JsonObject ob = jsons.getAsJsonObject();
		Set<Entry<String, JsonElement>> ss = ob.entrySet();
		for (Entry<String, JsonElement> entry : ss) {
			Station item = new Station();
			item.setCode(entry.getKey());
			item.setName(entry.getValue().getAsString());
			datas.add(item);

		}
		return datas;
	}
}
